package com.lu.common.utils;

import com.sun.mail.util.MailSSLSocketFactory;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

public class UnMailUtils implements Runnable {

    private static String from = "showluu@163.com";// 发件人电子邮箱;
    private static String fromPassword = "showlu19970108xx";// 发件人邮箱账号的授权码
    private static String host = "smtp.163.com"; // 指定发送邮件的主机smtp.qq.com(QQ)|smtp.163.com(网易)
    private static String hostProperty = "mail.smtp.host";// 邮件服务器
    private static String authProperty = "mail.smtp.auth";// 邮件服务器认证

    private String email;//收件人邮箱账号
    private String code;//发件内容

    public UnMailUtils(String email, String subject, String code) {
        this.email = email;
        this.code = code;
    }

    public UnMailUtils(String email, String code) {
        this.email = email;
        this.code = code;
    }

    public void run() {
        // 1.创建连接对象javax.mail.Session
        // 2.创建邮件对象 javax.mail.Message
        // 3.发送一封激活邮件
        Properties properties = System.getProperties();// 获取系统属性

        properties.setProperty(hostProperty, host);// 设置邮件服务器
        properties.setProperty(authProperty, "true");// 打开认证

        try {
            //QQ邮箱需要下面这段代码，163邮箱不需要
            MailSSLSocketFactory sf = new MailSSLSocketFactory();
            sf.setTrustAllHosts(true);
            properties.put("mail.smtp.ssl.enable", "true");
//            properties.put("mail.smtp.ssl.socketFactory", sf);

            // 1.获取默认session对象
            Session session = Session.getDefaultInstance(properties, new Authenticator() {
                public PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(from, fromPassword);
                }
            });
            session.setDebug(true);
            // 2.创建邮件对象
            Message message = new MimeMessage(session);
            // 2.1设置发件人
            message.setFrom(new InternetAddress(from));
            // 2.2设置接收人
            message.addRecipient(Message.RecipientType.TO, new InternetAddress(email));
            // 2.3设置邮件主题
            message.setSubject("定时提醒");
            String content = "<html><head></head><body><h1>早啊,起来了！------"+code+"</h1><h3><a href=''>sk.showlu.top"+
                    "</a></h3></body></html>";
            message.setContent(content, "text/html;charset=UTF-8");
            // 2.4设置邮件内容
//            String content = "您的验证码是："+code;
//            message.setContent(content, "text/html;charset=UTF-8");
//            message.setText(content);
            // 3.发送邮件
            Transport.send(message);
            System.out.println("邮件成功发送!");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        new UnMailUtils("1309617271@qq.com", "1212").run();
    }
}
//</editor-fold>
